﻿using AccompanyCabinet.Helper;
using System;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace AccompanyCabinet
{
    public partial class Login : Form
    {
        private Admin.Index index;
        public Login()
        {
            InitializeComponent();
        }
        private static string Md5Hash(string input)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            KeyBoard.Close();
            string loginNo = this.loginNo.Text;
           // string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.pwd.Text, "MD5").ToLower();
            string pwd = Md5Hash(this.pwd.Text).ToLower();
            string url = "TerminalInfo/Login";
            var res = HttpHelper.Get(url+ "?loginNo="+loginNo+"&pwd="+pwd+"");
            if (res.status=="OK")
            {
                index = new Admin.Index();
                this.Hide();
                index.Show();
            }
            else
            {
                MessageBox.Show(res.Message);
            }          
        }

        private void Login_Load(object sender, EventArgs e)
        {
            loginNo.Focus();
        }
        CabinetControl.KeyBoard KeyBoard = CabinetControl.KeyBoard.CreateInstrance();
        private void loginNo_MouseClick(object sender, MouseEventArgs e)
        {
            CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
            kb.Location = new Point(PointToScreen(loginNo.Location).X+65,PointToScreen(loginNo.Location).Y+65);
            loginNo.Focus();
            loginNo.Invoke(new Action(()=> { kb.Show(); }));
            Activate();
        }

        private void pwd_TextChanged(object sender, EventArgs e)
        {

        }

        private void pwd_MouseClick(object sender, MouseEventArgs e)
        {
            CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
            kb.Location = new Point(PointToScreen(pwd.Location).X + 65, PointToScreen(pwd.Location).Y + 65);
            pwd.Focus();
            pwd.Invoke(new Action(() => { kb.Show(); }));
            Activate();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Front.index index = new Front.index();
            index.Show();
            this.Close();
        }
    }
}
